package at.jsug.topicvoting.gwt1.client.remote;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.http.client.Request;
import com.google.gwt.user.client.Window;
import com.kaboomerang.gwt.rest.client.handler.CollectionResponseHandler;

import at.jsug.topicvoting.gwt1.client.remote.rest.FetchTopicsRequest;
import at.jsug.topicvoting.gwt1.client.vo.Topic;

public class DataFacadeImpl extends AbstractDataService {


	public List<Topic> getTopics() {
		final Wrapper<List<Topic>> result = new Wrapper<List<Topic>>();
		
		new FetchTopicsRequest().send(new CollectionResponseHandler() {

			public void onSuccess(Collection resources) {
				//final TopicsResource topicsResource = (TopicsResource) resource;
				System.out.println("DataFacadeImpl.getTopics.onSuccess; resources="+resources);
				List<Topic> tmp = new LinkedList<Topic>();
				tmp.add(Topic.newNeedTopic(1, "tmp from DataFacadeImpl", 1, false));
				result.x = tmp;
				// Window.alert("Topics resource #"+topic.getId()+": " + topic.getTitle());
			}

			public void onError(Request request, Throwable exception) {
				Window.alert("Something went wrong: " + exception.getMessage());
				System.out.println("request: " + request);
				System.out.println();
				exception.printStackTrace();
			}
			
		});
		System.out.println("result.x = " + result.x);
		return result.x;
	}

	public void saveOrUpdateTopic(Topic topic) {
		// TODO Auto-generated method stub

	}

	private static class Wrapper<X> {
		public X x;
	}
}
